package liumengbaio.lolzhangyoubao.Activity.Fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import liumengbaio.lolzhangyoubao.Activity.Adapter.Hero3DGridViewAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.FreeHeroInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.Hero3DInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/6.
 */
public class Fragment3D extends Fragment {
    private Hero3DInfo hero3DInfo;
    private Hero3DGridViewAdapter gridViewAdapter;
    private GridView gridView;
    private String id;

    public static Fragment3D newInstants(FreeHeroInfo info) {
        Fragment3D fragment3D = new Fragment3D();
        Bundle bundle = new Bundle();
        bundle.putSerializable("info3d",info);
        fragment3D.setArguments(bundle);
        return fragment3D;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_3d, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FreeHeroInfo info3d= (FreeHeroInfo) getArguments().getSerializable("info3d");
        id = info3d.getId();
        initView(getView());
        initData();
        getDatafromWeb();

    }


    private void initView(View view) {
        gridView = (GridView) view.findViewById(R.id.gridview3d);
        hero3DInfo = new Hero3DInfo();
    }

    private void initData() {

    }


    public void getDatafromWeb() {
        String url = "http://lol.zhangyoubao.com/apis/rest/RolesService/new_paper_skin?roleid=" + id + "&i_=EsgMWh3USt5BgC/cGtS1hKY9wRoLT+mJ6iTQiBqMiVY=&deviceId=EsgMWh3USt5BgC/cGtS1hKY9wRoLT+mJ6iTQiBqMiVY=&t_=1473317696299&p_=21242&v_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=18&cha=ucMartket&u_=&modle_=Coolpad%205311&api=RolesService.new_paper_skin&time=1473317696&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=488428&secretSignature=VzgcSHd47S2KyNy7gK65soGWiQw=";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                String result = "";
                result = response.body().string();
                Gson gson = new Gson();
                hero3DInfo = gson.fromJson(result, Hero3DInfo.class);
                if (hero3DInfo.getData().size() > 0) {
                    handler.sendEmptyMessage(1);
                } else {
                    handler.sendEmptyMessage(0);
                }
            }
        });


    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "网络请求失败!", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    gridViewAdapter = new Hero3DGridViewAdapter(hero3DInfo, getActivity());
                    gridView.setAdapter(gridViewAdapter);

                    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                            View dialogView = View.inflate(getActivity(), R.layout.view_dialog, null);
                            LinearLayout ll_skin = (LinearLayout) dialogView.findViewById(R.id.ll_skin);
                            LinearLayout ll_model = (LinearLayout) dialogView.findViewById(R.id.ll_model);
                            LinearLayout ll_video = (LinearLayout) dialogView.findViewById(R.id.ll_video);
                            LinearLayout ll_cancel = (LinearLayout) dialogView.findViewById(R.id.ll_cancel);
                            if (hero3DInfo.getData().get(position).getTexPath() == null) {
                                ll_model.setVisibility(View.GONE);
                            }
                            builder.setView(dialogView);
                            builder.setCancelable(true);


                            final AlertDialog ad = builder.create();//将对话框创建出来

                            ll_skin.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String skinurl = hero3DInfo.getData().get(position).getPic_url();
                                }
                            });
                            ll_model.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String modelurl = hero3DInfo.getData().get(position).getTexPath();

                                }
                            });
                            ll_video.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String videourl = hero3DInfo.getData().get(position).getMovie_url();


                                }
                            });
                            ll_cancel.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ad.cancel();
                                }
                            });
                            ad.show();
//                            dialogView.setOnTouchListener(new View.OnTouchListener() {
//                                @Override
//                                public boolean onTouch(View v, MotionEvent event) {
//                                    ad.dismiss();
//                                    return false;
//                                }
//                            });

                        }
                    });

                    break;
            }
        }
    };
}
